package com.mengchengnet.chaperonage.view;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.mengchengnet.chaperonage.R;


/**
 * Created by Administrator on 15-12-24.
 */
public class CustomizedProgressDialog extends Dialog {

    private static CustomizedProgressDialog dialog;

    public CustomizedProgressDialog(Context context) {
        super(context);
    }

    public CustomizedProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    /**
     * 当窗口焦点改变时调用
     */
//    public void onWindowFocusChanged(boolean hasFocus) {
//        ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);
//        // 获取ImageView上的动画背景
////        AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground();
////        // 开始动画
////        spinner.start();
//
//        Animation animation = AnimationUtils.loadAnimation(this,
//                R.anim.loading);
//        // 显示动画
//        imageView.startAnimation(animation);
//    }

    /**
     * 给Dialog设置提示信息
     *
     * @param message
     */
    public void setMessage(CharSequence message) {
        if (message != null && message.length() > 0) {
            findViewById(R.id.message).setVisibility(View.VISIBLE);
            TextView txt = (TextView) findViewById(R.id.message);
            txt.setText(message);
            txt.invalidate();
        }
    }

    /**
     * 弹出自定义ProgressDialog
     *
     * @param context        上下文
     * @param message        提示
     * @param cancelable     是否按返回键取消
     * @param cancelListener 按下返回键监听
     * @return
     */
    public static CustomizedProgressDialog showDialog(Context context, CharSequence message, boolean cancelable, OnCancelListener cancelListener) {
//        CustomizedProgressDialog dialog = new CustomizedProgressDialog(context, R.style.Custom_Progress);
        if (dialog != null) {
            hideDialog();
        }
        dialog = new CustomizedProgressDialog(context);
        dialog.setCanceledOnTouchOutside(false);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.customized_progress_dialog, null);

        ImageView imageView = (ImageView) layout.findViewById(R.id.spinnerImageView);
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.loading);
        LinearInterpolator interpolator = new LinearInterpolator();
        animation.setInterpolator(interpolator);
        imageView.startAnimation(animation);

        if (message == null || message.length() == 0) {
            layout.findViewById(R.id.message).setVisibility(View.GONE);
        } else {
            TextView txt = (TextView) layout.findViewById(R.id.message);
            txt.setText(message);
        }
        // 按返回键是否取消
        dialog.setCancelable(cancelable);
        // 监听返回键处理
        dialog.setOnCancelListener(cancelListener);

        Window window = dialog.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        dialog.show();
        window.setContentView(layout);
        return dialog;
    }

    public static void hideDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}
